#include "include/gudb.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("=== Testing Basic GuDB Functions ===\n");
    
    // Clean up any existing test database
    system("rm -rf ./test_basic_db");
    
    // Test 1: Open database
    printf("1. Opening database...\n");
    gudb* db = gudb_open("./test_basic_db", GU_OPEN_CREATE, "admin", "admin");
    if (!db) {
        printf("FAIL: Cannot open database - %s\n", gudb_error(db));
        return 1;
    }
    printf("SUCCESS: Database opened\n");
    
    // Test 2: Begin transaction
    printf("\n2. Beginning transaction...\n");
    gudb_txn* txn = gudb_begin(db, GU_TXN_RC, "gudb");
    if (!txn) {
        printf("FAIL: Cannot begin transaction - %s\n", gudb_error(db));
        gudb_close(db);
        return 1;
    }
    printf("SUCCESS: Transaction started (current database: gudb)\n");
    
    // Test 3: Show databases
    printf("\n3. Testing SHOW DATABASES...\n");
    result_t* result = NULL;
    if (gudb_exec(txn, "SHOW DATABASES", NULL, &result) == GU_OK) {
        printf("SUCCESS: SHOW DATABASES worked\n");
        printf("Databases found: %u\n", result->row_count);
        for (uint32_t i = 0; i < result->row_count; i++) {
            printf("  - %s\n", result->rows[i][0]);
        }
        gudb_free_result(result);
    } else {
        printf("FAIL: SHOW DATABASES failed - %s\n", gudb_txn_error(txn));
    }
    
    // Test 4: Create database
    printf("\n4. Testing CREATE DATABASE...\n");
    if (gudb_exec(txn, "CREATE DATABASE testdb", NULL, &result) == GU_OK) {
        printf("SUCCESS: CREATE DATABASE worked\n");
        if (result) gudb_free_result(result);
    } else {
        printf("FAIL: CREATE DATABASE failed - %s\n", gudb_txn_error(txn));
    }
    
    // Test 5: Show databases again
    printf("\n5. Testing SHOW DATABASES again...\n");
    if (gudb_exec(txn, "SHOW DATABASES", NULL, &result) == GU_OK) {
        printf("SUCCESS: SHOW DATABASES worked\n");
        printf("Databases found: %u\n", result->row_count);
        for (uint32_t i = 0; i < result->row_count; i++) {
            printf("  - %s\n", result->rows[i][0]);
        }
        gudb_free_result(result);
    } else {
        printf("FAIL: SHOW DATABASES failed - %s\n", gudb_txn_error(txn));
    }
    
    // Test 6: USE database
    printf("\n6. Testing USE DATABASE...\n");
    if (gudb_exec(txn, "USE testdb", NULL, &result) == GU_OK) {
        printf("SUCCESS: USE DATABASE worked\n");
        if (result) gudb_free_result(result);
    } else {
        printf("FAIL: USE DATABASE failed - %s\n", gudb_txn_error(txn));
    }
    
    // Test 7: Show tables in new database
    printf("\n7. Testing SHOW TABLES in testdb...\n");
    if (gudb_exec(txn, "SHOW TABLES", NULL, &result) == GU_OK) {
        printf("SUCCESS: SHOW TABLES worked\n");
        printf("Tables found: %u\n", result->row_count);
        for (uint32_t i = 0; i < result->row_count; i++) {
            printf("  - %s\n", result->rows[i][0]);
        }
        if (result->row_count == 0) {
            printf("  (no tables - correct for new database)\n");
        }
        gudb_free_result(result);
    } else {
        printf("FAIL: SHOW TABLES failed - %s\n", gudb_txn_error(txn));
    }
    
    // Test 8: Create table
    printf("\n8. Testing CREATE TABLE...\n");
    if (gudb_exec(txn, "CREATE TABLE users (id INT, name VARCHAR(50))", NULL, &result) == GU_OK) {
        printf("SUCCESS: CREATE TABLE worked\n");
        if (result) gudb_free_result(result);
    } else {
        printf("FAIL: CREATE TABLE failed - %s\n", gudb_txn_error(txn));
    }
    
    // Test 9: Show tables again
    printf("\n9. Testing SHOW TABLES after creating table...\n");
    if (gudb_exec(txn, "SHOW TABLES", NULL, &result) == GU_OK) {
        printf("SUCCESS: SHOW TABLES worked\n");
        printf("Tables found: %u\n", result->row_count);
        for (uint32_t i = 0; i < result->row_count; i++) {
            printf("  - %s\n", result->rows[i][0]);
        }
        gudb_free_result(result);
    } else {
        printf("FAIL: SHOW TABLES failed - %s\n", gudb_txn_error(txn));
    }
    
    printf("\n=== Test Summary ===\n");
    printf("Basic database operations test completed.\n");
    
    // Cleanup
    gudb_commit(txn);
    gudb_close(db);
    system("rm -rf ./test_basic_db");
    
    return 0;
}